package com.yellowpage.ca.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.ca.dao.RoleDao;
import com.yellowpage.ca.dao.RoleFunctionDao;
import com.yellowpage.ca.dao.UserRoleDao;
import com.yellowpage.ca.data.Role;
import com.yellowpage.ca.data.RoleFunction;
import com.yellowpage.ca.data.UserRole;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class RoleServiceImpl extends BaseServiceImpl implements RoleService {
	private RoleDao roleDao;
	private UserRoleDao userRoleDao;
	private RoleFunctionDao roleFunctionDao;

	/* white generate Method */
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	public List<Role> getAllRole() {
		return roleDao.getAll();
	}

	public List<Map> queryRoleByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT a.ROLE_ID , a.NAME , a.TYPE , a.STATUS,  "
								+ "       (select display_name from CA_USER where user_id = a.last_update_user) as last_update_user, "
								+ "       date_format(a.last_update_time,'%Y-%m-%d %T') as last_update_time, "
								+ "       (select display_name from CA_USER where user_id = a.create_user) as create_user,"
								+ "       date_format(a.create_time,'%Y-%m-%d %T') as create_time "
								+ "  FROM CA_ROLE a WHERE 1=1 ")
				.addCondition(" and NAME like :name ")
				.addCondition(" and TYPE = :type ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveRole(Role role) {
		roleDao.save(role);
	}

	public void updateRole(Role role) {
		roleDao.update(role);
	}

	public void delete(List deleteList) {
		roleDao.remove(deleteList);
	}

	public List<Map> getUserWithRole(Long roleId, boolean isRoleUser) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select u.user_id, u.name, u.display_name "
						+ " from CA_USER u "
						+ "  where "
						+ (isRoleUser ? "" : "not")
						+ " exists(select ur.user_role_id from CA_USER_ROLE ur "
						+ " where  ur.user_id = u.user_id and ur.role_id = "
						+ roleId + ")");

		List<Map> resultList = sqlAssistant
				.query(baseDao.getConnection(), null);

		return resultList;
	}

	public void saveRoleUsers(List<Long> userIdList, Long roleId,
			Long loginUserId) {

			List<Long> userRoleIds = userRoleDao.getUserRoleIdByRole(roleId);
			userRoleDao.remove(userRoleIds);

			List<UserRole> userRoles = new ArrayList();
			Date createTime = new Date();
			for (Long userId : userIdList) {
				UserRole userRole = new UserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleId);
				userRole.setCreateTime(new Timestamp(createTime.getTime()));
				userRole.setCreateUser(loginUserId);

				userRoles.add(userRole);
			}

			userRoleDao.save(userRoles);

	
	}

	public List<Map> getFunctionWithRole(Long roleId, boolean isRoleFunc,
			Map queryMap) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"select f.function_id, f.name  "
								+ " from CA_FUNCTION f "
								+ "  where "
								+ (isRoleFunc ? "" : "not")
								+ " exists(select rf.role_function_id from CA_ROLE_FUNCTION rf "
								+ " where rf.function_id = f.function_id and rf.role_id =  "
								+ roleId + ")").addCondition(
						" and f.func_module_id = :funcModuleId ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				queryMap);

		return resultList;
	}

	public void saveRoleFunctions(List<Long> functionIdList, Long roleId,
			Long loginUserId, Long funcModuleId) {


			List<Long> roleFunctionIds = roleFunctionDao
					.getRoleFunctionIdByRole(roleId, funcModuleId);
			roleFunctionDao.remove(roleFunctionIds);

			List<RoleFunction> roleFunctions = new ArrayList();
			Date createTime = new Date();
			for (Long functionId : functionIdList) {
				RoleFunction roleFunc = new RoleFunction();
				roleFunc.setFunctionId(functionId);
				roleFunc.setRoleId(roleId);
				roleFunc.setCreateTime(new Timestamp(createTime.getTime()));
				roleFunc.setCreateUser(loginUserId);

				roleFunctions.add(roleFunc);
			}

			roleFunctionDao.save(roleFunctions);

		
	}

	public RoleFunctionDao getRoleFunctionDao() {
		return roleFunctionDao;
	}

	public void setRoleFunctionDao(RoleFunctionDao roleFunctionDao) {
		this.roleFunctionDao = roleFunctionDao;
	}

	public UserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

}